<?php 
require 'functions.php';

$hasError = false;

if(!isset($_SESSION['user'])||empty($_SESSION['user'])){
	$hasError = true;
  $errorBag['error'] = '登陆后才能留言';
}

$user = $_SESSION['user'];
$username = $user['user_name'];
$content = input('content');
if (trim($content) == '') {
  $hasError = true;
  $errorBag['error'] = '留言信息不能为空';
}

if ($hasError) {
  $_SESSION['has_error'] = $hasError;
  $_SESSION['error_bag'] = $errorBag;
  header('Location:'. url('index'));
  return;
}


$sql = 'insert into messages(user_name,content,created_at) values(?,?,now())';
execute($sql, [$username, $content]);
header('Location:'. url('index'));